import { createRouter, createWebHistory } from 'vue-router';
import LoginPage from '@/views/LoginPage.vue';
import DashboardPage from '@/views/DashboardPage.vue';
import UserManagementPage from '@/views/UserManagementPage.vue';
import AdminManagementPage from '@/views/AdminManagementPage.vue';
import SettingsPage from '@/views/SettingsPage.vue';
import HomePage from '@/views/HomePage.vue';
import CodeRepository from "@/components/CodeRepository.vue";
import ContactDevelopers from "@/components/ContactDevelopers.vue";
import Features from "@/components/FeaturesPage.vue"
import AndroidDownload from "@/components/AndroidDownload.vue";
import UserBookingManagementPage from "@/views/UserBookingManagementPage.vue";
import BookkeepingRecords from "@/views/BookkeepingRecords.vue";

const routes = [
    {
        path: '/',
        redirect: '/login'
    },
    {
        path: '/login',
        name: 'Login',
        component: LoginPage,
        meta: { requiresAuth: false }
    },
    {
        path: '/dashboard',
        name: 'Dashboard',
        component: DashboardPage,
        meta: { requiresAuth: true },
        children: [
            {
                path: 'homepage',
                name: 'HomePage',
                component: HomePage
            },
            {
                path: 'user-management',
                name: 'UserManagement',
                component: UserManagementPage
            },
            {
                path: 'user-booking-management',
                name: 'UserBookingManagement',
                component: UserBookingManagementPage
            },
            {
                path: 'admin-management',
                name: 'AdminManagement',
                component: AdminManagementPage
            },
            {
                path: 'settings',
                name: 'Settings',
                component: SettingsPage
            },
            {
                path: '',
                redirect: { name: 'HomePage' }
            }
        ]
    },
    {
        path: '/users/:account/bookkeeping',
        name: 'BookkeepingRecords',
        component: BookkeepingRecords,
        props: true
    },
    {
        path: '/code-repo',
        component: CodeRepository,
        name: 'CodeRepository',
        meta: {
            requiresAuth: false
        }
    },
    {
        path: '/contact-dev',
        component: ContactDevelopers,
        name: 'ContactDevelopers',
        meta: {
            requiresAuth: false
        }
    },
    {
        path: '/features',
        component: Features,
        name: 'Features',
        meta: {
            requiresAuth: false
        }
    },
    {
        path: '/android-download',
        component: AndroidDownload,
        name: 'AndroidDownload',
        meta: {
            requiresAuth: false
        }
    },
    {
        path: '/:pathMatch(.*)*',
        redirect: '/dashboard/homepage'
    }
];

const router = createRouter({
    history: createWebHistory(),
    routes
});

// 路由守卫
router.beforeEach((to, from, next) => {
    const isAuthenticated = localStorage.getItem('token');
    const requiresAuth = to.matched.some(record => record.meta.requiresAuth);

    if (requiresAuth && !isAuthenticated) {
        next('/login');
    } else if (to.path === '/login' && isAuthenticated) {
        next('/dashboard/homepage');
    } else {
        next();
    }
});

export default router;